<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.BormatDAO"%>
<%@page import="db.BackmatDAO"%>
<%@page import="db.TomatDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp"%>
<%
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	BormatDAO bormatDao = (BormatDAO)ctx.getBean("BormatDAO");
	BackmatDAO backmatDao = (BackmatDAO)ctx.getBean("BackmatDAO");
	TomatDAO tomatDao = (TomatDAO)ctx.getBean("TomatDAO");
	
	String searchSub = request.getParameter("searchSub");
	String searchType = request.getParameter("searchType");
	String searchKeyword = request.getParameter("searchKeyword");
	String searchDate = request.getParameter("searchDate");
	String pnStr = request.getParameter("pn");
	
	int pn = 0;
	int pageSize = 10;
	Integer teamId = (Integer)session.getAttribute("tid");
	
	int type = 1;
	
	if(pnStr != null && !"".equals(pnStr.trim())){
		try{
			pn = Integer.parseInt(pnStr);
		}catch(Exception e){
			pn = 0;
		}
	}
	
	List where = new ArrayList();
	
	where.add(new Object[]{"eq", "r.isDelete", 0});
	where.add(new Object[]{"eq", "b.teamId", teamId});
	
	if(searchSub != null && !"".equals(searchSub)){
		if(!"0".equals(searchSub))
			where.add(new Object[]{"eq", "r.subStore", Integer.parseInt(searchSub)});
	}
	
	if(searchType != null && !"".equals(searchType)){
		if("1".equals(searchType))
			where.add(new Object[]{"like", "bu.name", searchKeyword});
		else if("2".equals(searchType))
			where.add(new Object[]{"like", "b.borCode", searchKeyword});
		else if("3".equals(searchType))
			where.add(new Object[]{"like", "rm.code", searchKeyword});
		else if("4".equals(searchType))
			where.add(new Object[]{"like", "rm.name", searchKeyword});
		else if("5".equals(searchType))
			where.add(new Object[]{"like", "rpl.name", searchKeyword});
	}
	
	if(searchDate != null && !"".equals(searchDate)){
		Date dateVal = new SimpleDateFormat("yyyy-MM-dd").parse(searchDate);
		where.add(new Object[]{"eq", "b.okDate", dateVal});
	}
	
	Map map = bormatDao.getBormat(pn, pageSize,
			new String[][]{new String[]{"desc","date"}},
			where,
			new String[][]{new String[]{"","id"},new String[]{"","b.okDate"},new String[]{"","b.borCode"},new String[]{"","rm.code"},new String[]{"","rm.name"},new String[]{"","rm.company"},new String[]{"","okNum"},new String[]{"","r.purPrice"},new String[]{"","r.batch"},new String[]{"","okNum"},new String[]{"","okNum"},new String[]{"","rpl.name"},new String[]{"","rs.name"},new String[]{"","bpr.code"},new String[]{"","bpr.name"},new String[]{"","br.name"},new String[]{"","bc.name"},new String[]{"","bu.name"}}		
			,new String[]{"borreq","receivepurmat","place","material","substore","project","company","reccompany","user"});
	
	request.setAttribute("map",map);
	
	List data = (List)map.get("data");
	for(int i=0;i<data.size();i++){
		Object[] objects = (Object[])data.get(i);
		
		List backWhere = new ArrayList();
		backWhere.add(new Object[]{"eq", "bormatId", objects[0]});
		int backNum = backmatDao.getSum(backWhere,new String[]{});
		objects[9] = backNum;
		
		List toWhere = new ArrayList();
		toWhere.add(new Object[]{"eq", "bormatId", objects[0]});
		int toNum = tomatDao.getSum(toWhere,new String[]{});
		objects[10] = toNum;
	}
	
	int pageTotal = ((Integer)map.get("pageTotal")).intValue();
	int pageNo = ((Integer)map.get("pageNo")).intValue();
	List pageList=getPage(pageTotal,pageNo);
	request.setAttribute("pages",pageList);
	
	request.setAttribute("searchSub",searchSub);
	request.setAttribute("searchType",searchType);
	request.setAttribute("searchKeyword",searchKeyword);
	request.setAttribute("searchDate",searchDate);
	
	pageContext.forward("/vm/search/seahcBorrow.vm");
	
%>